<template>
    <div class="container">
        <!-- 导航栏区域开始 -->
        <TridentNavBar>
            <template #left><i class="iconfont icon-zuojiantou"></i></template>
            <template #default>服务通知</template>
        </TridentNavBar>
        <!-- 导航栏区域结束 -->

        <!-- 过滤区域开始 -->
        <div class="filter-wrap">
            <van-dropdown-menu active-color="#27BA9B">
                <!-- 类型 -->
                <van-dropdown-item @change="handleFilterNotify" v-model="notifyListParams.is_read" :options="readOptions" />
                
                <!-- 排序 -->
                <!-- <van-dropdown-item @change="handleSortNotify" v-model="notifyListParams.sort_type" :options="sortTypes" /> -->
            </van-dropdown-menu>
        </div>
        <!-- 过滤区域结束 -->

        <!-- 列表区域开始 -->
        <div class="content-wrap">
            <van-pull-refresh
                v-model="refreshing"
                @refresh="onRefresh"
                style="min-height: calc(100vh - 100px);"
            >
                <van-list
                    v-model:loading="loading"
                    :finished="finished"
                    :finished-text="totalPage > 1 ? '没有更多了' : ''"
                    @load="getNotifyList"
                    offset="50"
                    class="list-wrap"
                >
                    <!-- 骨架屏区域 -->
                    <div class="skeleton-item" v-for="(index) in 6" :key="index" v-if="isSkeleton">
                        <van-skeleton>
                            <template #template>
                                <div class="skeleton-box">
                                    <div class="title">
                                        <van-skeleton-paragraph row-width="20%" />
                                        <van-skeleton-paragraph row-width="20%" />
                                    </div>

                                    <div class="content">
                                        <van-skeleton-paragraph row-width="100%" />
                                        <van-skeleton-paragraph row-width="100%" style="margin: 5px 0;"/>
                                        <van-skeleton-paragraph row-width="100%" />
                                    </div>
                                    <div class="footer">
                                        <van-skeleton-paragraph row-width="36%" />
                                    </div>
                                </div>
                            </template>
                        </van-skeleton>
                    </div>

                    <template v-if="notifyList.length > 0">
                        <div class="item" v-for="(item, index) in notifyList" :key="index">
                            <!-- 头部区域开始 -->
                            <div class="header-box">
                                <div class="title">{{ item.title }}</div>
                                <div class="read btn" v-if="item.is_read == 0" @click="handleSetRead(item)">设置为已读</div>
                                <div class="read" v-else>已读</div>
                                
                                <div class="mark" v-if="item.is_read == 0"></div>
                            </div>

                            <!-- 内容区域开始 -->
                            <div class="content-box">
                                <div class="text-panel">{{ item.contents }}</div>
                                <div class="image-panel" v-if="item.image_url">
                                    <div class="image-item" v-for="(value, index) in item.image_url.split(',')" :key="index">
                                        <van-image
                                            width="80"
                                            height="80"
                                            fit="contain"
                                            :src="global.MINIO_FILE_DOMAIN + value"
                                            @click="handleImagePreview(global.MINIO_FILE_DOMAIN + value)"
                                        />
                                    </div>
                                </div>
                            </div>

                            <!-- 尾部区域开始 -->
                            <div class="footer-box">{{ item.create_time }}</div>
                        </div>
                    </template>
                    <template v-else>
                        <TridentEmpty v-if="!loading" message="暂无通知"/>
                    </template>

                    <!-- 返回顶部 -->
                    <van-back-top><i class="iconfont icon-top" style="font-size: 30px;"></i></van-back-top>
                </van-list>
            </van-pull-refresh>
        </div>
        <!-- 列表区域结束 -->
    </div>
</template>

<script lang="ts" setup>
import { reqEditNotifyAPI, reqNotifyListAPI } from '@/apis/notify'
import { useUserStore } from '@/store/modules/user'
import type { Notify, NotifyListParams } from '@/types/notify'
import { showImagePreview, showSuccessToast } from 'vant'
import { inject, reactive, ref } from 'vue'

// ----------------- 定义全局变量开始 -----------------
// 全局属性
const global: any = inject('global')

// 骨架屏
const isSkeleton = ref<boolean>(false)

// 用户存储
const userStore = useUserStore()
// ----------------- 定义全局变量结束 -----------------



// ----------------- 下拉刷新功能开始 -----------------
const refreshing = ref<boolean>(false)
const onRefresh = async () => {
    // 清空列表数据
    finished.value = false

    // 将 loading 设置为 true，表示处于加载状态
    loading.value = true

    notifyListParams.page = 1

    await getNotifyList()
}
// ----------------- 下拉刷新功能结束 -----------------



// --------------- 处理通知列表功能开始 ---------------
const loading = ref(false)

// 完成
const finished = ref(false)

// 通知列表参数
const notifyListParams = reactive<NotifyListParams>({
    uid: userStore.profile.user.uid,
    private_public: 1,
    is_read: 'all',
    page: 1,
})

// 通知列表
const notifyList = ref<Notify[]>([])

// 总条数
const total = ref<number>(0)

// 总页数
const totalPage = ref<number>(0)

// 获取通知列表
const getNotifyList = async () => {
    isSkeleton.value = true
    if (refreshing.value) {
        notifyList.value = []
        refreshing.value = false
    }

    loading.value = true

    const result = await reqNotifyListAPI(notifyListParams)
    notifyList.value.push(...result.data.notifies)
    total.value = result.data.total
    totalPage.value = result.data.total_page
    
    if (notifyListParams.page < result.data.total_page) {
        notifyListParams.page++
    } else {
        finished.value = true
    }

    loading.value = false
    isSkeleton.value = false
}
// ---------------- 处理通知列表功能结束 ----------------

// 处理图片预览
const handleImagePreview = (image_url: string) => {
    showImagePreview({images: [image_url], closeable: true})
}




// ---------------- 处理设置已读功能开始 ----------------
const handleSetRead = async (item: Notify) => {
    await reqEditNotifyAPI({
        id: item.id,
        is_read: '1'
    })
    item.is_read = 1

    showSuccessToast('设置成功')
}
// ---------------- 处理设置已读功能结束 ----------------



// ------------------ 处理筛选功能开始 ------------------
const readOptions = ref([
    {
        text: '全部',
        value: 'all'
    },
    {
        text: '未读',
        value: '1'
    },
    {
        text: '已读',
        value: '2'
    },
])

// 过滤通知
const handleFilterNotify = async (value: string) => {
    notifyList.value = []
    notifyListParams.page = 1
    notifyListParams.is_read = value

    // 当加载完成设置为false,将会自动调用getMessageList()方法获取数据
    // 但是第一页数据需要手动getMessageList()
    finished.value = false

    // 当获取第一页数据的时候需要手动调用接口
    // 非第一页数据则无需手动调用接口,会自动调用接口
    if (notifyListParams.page == 1) {
        await getNotifyList()
    }
}
// ------------------ 处理筛选功能结束 ------------------
</script>

<style scoped lang="scss">
page,
.container {
    height: 100%;

    // 过滤区域开始
    .filter-wrap {
        position: fixed;
        top: 45px;
        left: 0;
        width: 100%;
        z-index: 100;
    }
    // 过滤区域结束

    // 列表区域开始
    .content-wrap {
        padding-top: 100px;
        .list-wrap {
            padding: 0 10px;
            margin-bottom: 10px;
            .item {
                background-color: #fff;
                padding: 15px;
                border-radius: 10px;
                margin-top: 5px;

                // 头部区域开始
                .header-box {
                    display: flex;
                    justify-content: space-between;
                    position: relative;
                    .title {
                        font-size: 16px;
                    }
                    .read {
                        font-size: 14px;
                        color: #929090;
                    }
                    .btn {
                        border: 1px solid #27BA9B;
                        color: #27BA9B;
                        padding: 5px;
                        border-radius: 2px;
                    }
                    // 标记区域开始
                    .mark {
                        background-color: red;
                        position: absolute;
                        width: 12px;
                        height: 12px;
                        border-radius: 8px;
                        top: -10px;
                        left: -14px;
                    }
                }

                // 内容区域开始
                .content-box {
                    margin: 10px 0px;
                    border-top: 1px solid #e7e7e7;
                    border-bottom: 1px solid #e7e7e7;
                    padding: 10px 0px;
                    .text-panel {
                        font-size: 14px;
                        line-height: 18px;
                    }
                    .image-panel {
                        display: flex;
                        flex-wrap: wrap;
                        .image-item {
                            width: 85px;
                            height: 85px;
                            display: flex;
                            align-items: center;
                            justify-content: center;
                            border: 1px solid #e7e7e7;
                            margin-right: 8px;
                            border-radius: 5px;
                            margin-top: 10px;
                            &:nth-child(4n) {
                                margin-right: 0px;
                            }
                        }
                    }
                }

                // 尾部区域开始
                .footer-box {
                    display: flex;
                    justify-content: flex-end;
                    color: #d3d2d2;
                    font-size: 12px;
                }
            }

            // 骨架屏区域开始
            .skeleton-item {
                background-color: #fff;
                border-radius: 10px;
                margin-top: 5px;
                .skeleton-box {
                    padding: 15px;
                    width: 100%;
                    .title {
                        display: flex;
                        align-items: center;
                        padding-bottom: 6px;
                        justify-content: space-between
                    }

                    .content {
                        border-top: 1px solid #f1f1f1;
                        padding-top: 10px;
                    }
                    .footer {
                        padding-top: 10px;
                        display: flex;
                        justify-content: flex-end;
                    }
                }
            }
        }
    }
}
</style>